// pages/pay/index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        pay_type: "1",
        order_sn: '',
        pay_total: 0,
        order_type: 1,
        submit_btn_fal: false,
        num: 0.01,
        balance: '1'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options, 'pages/pay/index.js');
        var that = this;
        var m_sn = '';
        var balance = ''
        if (typeof (options.sn) != "undefined") m_sn = options.sn;
        var m_type = 1;
        if (typeof (options.type) != "undefined") m_type = options.type;
        var m_total = 99999;
        if (typeof (options.total) != "undefined") m_total = options.total;
        if (typeof (options.isbalance) != "undefined") {}
        balance = options.isbalance;
        that.setData({
            pay_total: m_total,
            order_sn: m_sn,
            order_type: m_type,
            balance: balance
        });
        //用户授权登录
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo
            });
            that.toPay();
        });
    },
    onShow: function (options) {
        console.log(options, '页面options')
    },

    //支付界面
    toPay: function () {
        var that = this;
        var m_type = that.data.order_type;
        if (m_type == 1) { //订单支付
            wx.setNavigationBarTitle({
                title: '订单支付'
            })
            wx.request({
                url: rootDocment + '/api/comm_get/getOrderPrice',
                data: {
                    sn: that.data.order_sn,
                    user_id: that.data.userInfo.userID
                },
                method: 'GET',
                header: {},
                success: function (res) {
                    console.log(res.data);
                    if (res.data.code == 1001) {
                        that.setData({
                            pay_total: res.data.data.total
                        });
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '此订单不存在或已支付！',
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                }
                            }
                        })
                    }
                }
            })
        } else { //2:充值
            wx.setNavigationBarTitle({
                title: '会员充值'
            })
        }
    },

    //切换支付方式
    changePay: function (e) {
        var that = this;
        that.setData({
            pay_type: e.detail
        });
    },

    //稍后支付
    payLater: function () {
        var that = this;
        if (that.data.order_type == 1) {
            app.gourl('user/myorder', 'type=0');
        } else {
            wx.navigateBack({
                delta: 1
            })
        }
    },

    //立即支付
    payNow: function () {
        var that = this;

        console.log('支付')

        this.setData({
            submit_btn_fal: true
        })
        var that = this;
        var paraArr = new Array();
        paraArr['order_sn'] = that.data.order_sn;
        var sign = app.signature(paraArr);
        console.log(that.data.order_sn, that.data.pay_total, 'order_sn')
        if (that.data.pay_type == 1) { //微信支付
            wx.openEmbeddedMiniProgram({
                appId: 'wx3e64b506e01256fd',
                //M1791331011611729920
                path: `/pages/pay/pay?storeId=${'M1747810615612809216'}&orderNo=${that.data.order_sn}&orderAmount=${that.data.pay_total}&public=1`,
                envVersion: 'release', //B的类型  trial体验版, ‘release’正式版    ‘develop’开发版
                success: function () {
                    app.globalData.isSuccess = '1'
                    app.globalData.pay_order_no = that.data.order_sn;
                    console.log('跳转小程序成功');
                    that.setData({
                        submit_btn_fal: false
                    })
                    wx.request({
                        url: rootDocment + '/api/Query',
                        data: {
                            order_sn: paraArr['order_sn'],
                            sign: sign
                        },
                        method: 'POST',
                        header: {},
                        success: function (res) {
                            console.log('res支付跳转订单查询/api/Query', res)
                        }
                    })
                    wx.request({
                        url: rootDocment + '/api/query/cross',
                        data: {
                            order_sn: paraArr['order_sn'],
                            sign: sign
                        },
                        method: 'POST',
                        header: {},
                        success: function (res) {
                            console.log('res支付跳转订单轮询查询/api/query/cross', res)
                        }
                    })
                },
                fail: function () {
                    console.log('跳转小程序失败');
                    that.setData({
                        submit_btn_fal: false
                    })
                }
            })
            return
            paraArr['order_sn'] = that.data.order_sn;
            paraArr['openid'] = that.data.userInfo.openID;
            paraArr['total'] = that.data.pay_total;
            paraArr['uid'] = that.data.userInfo.userID;
            paraArr['order_type'] = that.data.order_type;
            var sign = app.signature(paraArr);
            wx.request({
                url: rootDocment + '/api/miniapp_pay/wxPay',
                data: {
                    order_sn: paraArr['order_sn'],
                    openid: paraArr['openid'],
                    total: paraArr['total'],
                    uid: paraArr['uid'],
                    order_type: paraArr['order_type'],
                    sign: sign
                },
                method: 'GET',
                header: {},
                success: function (res) {
                    console.log(res.data);
                    wx.requestPayment({
                        'timeStamp': res.data.timeStamp,
                        'nonceStr': res.data.nonceStr,
                        'package': res.data.package,
                        'signType': 'MD5',
                        'paySign': res.data.paySign,
                        'success': function (res) {
                            if (that.data.order_type == 1) {
                                app.redirect('user/myorder', 'type=1');
                                that.setData({
                                    submit_btn_fal: false
                                })
                            } else {
                                app.gotaburl('user/index');
                                that.setData({
                                    submit_btn_fal: false
                                })
                            }
                        },
                        'fail': function (res) {
                            console.log(res);
                            that.setData({
                                submit_btn_fal: false
                            })
                        }
                    })

                }
            })
        } else if (that.data.pay_type == 3) { //余额支付
            var paraArr1 = new Array();
            paraArr1['sn'] = that.data.order_sn;
            paraArr1['user_id'] = that.data.userInfo.userID;
            var sign = app.signature(paraArr1);
            wx.request({
                url: rootDocment + '/api/miniapp_pay/balancePay',
                data: {
                    sn: paraArr1['sn'],
                    user_id: paraArr1['user_id'],
                    sign: sign
                },
                method: 'POST',
                header: {},
                success: function (res) {
                    if (res.data.code == 1001) {
                        that.setData({
                            submit_btn_fal: false
                        })
                        app.redirect('user/myorder', 'type=1');
                    } else {
                        that.setData({
                            submit_btn_fal: false
                        })
                        wx.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false
                        })
                    }
                }
            })
        } else if (that.data.pay_type == 4) { //货到付款
            paraArr['sn'] = that.data.order_sn;
            paraArr['user_id'] = that.data.userInfo.userID;
            var sign = app.signature(paraArr);
            wx.request({
                url: rootDocment + '/api/miniapp_pay/codPay',
                data: {
                    sn: paraArr['sn'],
                    user_id: paraArr['user_id'],
                    sign: sign
                },
                method: 'POST',
                header: {},
                success: function (res) {
                    if (res.data.code == 1001) {
                        app.redirect('user/myorder', 'type=1');
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false
                        })
                    }
                }
            })
        }
    },

})